package com.wpen.unit.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.context.annotation.Configuration;

import com.wpen.unit.AppVariables;
import com.wpen.unit.service.RegistryService;

import lombok.extern.slf4j.Slf4j;

/**
 * 应用启动后，立刻执行内容
 * 
 * @author Wang Peng
 * @date 2022-12-06
 */
@Configuration
@Slf4j
public class AppStartedRunner implements ApplicationRunner {

	@Autowired
	private RegistryService registryService;

	@Override
	public void run(ApplicationArguments args) throws Exception {
		try {
			registryService.registryMe();
		} catch (Exception e) {
			log.error(e.getMessage(), e);
			// 应用强制停止，不能启动。
			log.info("Agent registration failed.");
			System.exit(1);
		}
		AppVariables.kernelStarted = true;
		log.info("Agent started successfully.");
	}

}
